package com.ruoyi.generator.service;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.generator.mapper.InspectionMapper;
import com.ruoyi.generator.po.*;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 业务字段 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class InspectionServiceImpl implements InspectionService
{
	@Autowired
	private InspectionMapper inspectionMapper;
	@Autowired
	private SysDeptMapper deptMapper;

	@Autowired
	private SysUserMapper userMapper;


	@Override
	public List<ScheduleShift> selectScheduleList() {

		List<WushuiShift> WushuiShift =inspectionMapper.selectScheduleList();
		return convertToScheduleShifts(WushuiShift);


	}

	@Override
	public AjaxResult saveEdit(ScheduleShift scheduleShift) {
		//根据period查找schedule表对应的id
		long ScheduleId = inspectionMapper.selectScheduleId(scheduleShift.getPeriod());

		for (WushuiShift Shift : scheduleShift.getShifts()) {
			WushuiShift wushuiShift = new WushuiShift();
			wushuiShift.setId(Shift.getId());
			wushuiShift.setName(Shift.getName());
			wushuiShift.setTime(Shift.getTime());
			inspectionMapper.saveEdit(wushuiShift);
		}
		return success();
	}

	@Override
	public String restore(String period) {
		long ScheduleId = inspectionMapper.selectScheduleId(period);
		//查询默认表中的数据
		List<WushuiShift> defaultShifts = inspectionMapper.selectDefaultShifts(ScheduleId);
		//删除schedule表中对应的数据
		inspectionMapper.deleteScheduleShifts(ScheduleId);
		//插入到schedule表中
		for (WushuiShift Shift : defaultShifts) {
			WushuiShift wushuiShift = new WushuiShift();
			wushuiShift.setName(Shift.getName());
			wushuiShift.setTime(Shift.getTime());
			wushuiShift.setScheduleId(Shift.getScheduleId());
			wushuiShift.setId(Shift.getId());
			inspectionMapper.save(wushuiShift);
		}

		return null;
	}

	@Override
	public List<Device> selectDeviceList(Device device) {
		return inspectionMapper.selectDeviceList(device);
	}

	@Override
	public List<SysDept> getDepts() {
		SysDept dept = new SysDept();
		dept.setParentId(101L);
		return deptMapper.selectDeptList(dept);
	}

	@Override
	public List<SysUser> getUsers() {
		SysUser user = new SysUser();
		return userMapper.selectUserList(user);
	}

	@Override
	public List<WushuiSchedule> getSchedules() {
		return inspectionMapper.getSchedules();
	}

	@Override
	public AjaxResult addCheckingPlan(CheckingPlan checkingPlan) {

		SpotCheckPlan spotCheckPlan = new SpotCheckPlan();
		spotCheckPlan.setArea(checkingPlan.getArea());
		spotCheckPlan.setPlanName(checkingPlan.getPlanName());
		spotCheckPlan.setDelFlag("0");
		spotCheckPlan.setPeriod(checkingPlan.getPeriod());
		List<String> Time = checkingPlan.getStartDate();
		String startTime = Time.get(0);
		String endTime = Time.get(1);
		spotCheckPlan.setStartTime(startTime);
		spotCheckPlan.setEndTime(endTime);
		spotCheckPlan.setStatus("0");
		spotCheckPlan.setRotate("0");
		//获取当前登录人
		String createBy = SecurityUtils.getUsername();;
		spotCheckPlan.setCreatedBy(createBy);

		List<Device> devices = checkingPlan.getSelecDevice();
		for (Device device : devices) {
			List<SysUser> users = checkingPlan.getSelecUser();
			spotCheckPlan.setDeviceId(String.valueOf(device.getId()));
			spotCheckPlan.setDeviceName(device.getDeviceName());
			spotCheckPlan.setDeviceLocation(device.getDeviceLocation());
			for (SysUser user : users) {
				spotCheckPlan.setUserId(String.valueOf(user.getUserId()));
				spotCheckPlan.setUserName(user.getUserName());
				spotCheckPlan.setDeptId(String.valueOf(user.getDeptId()));
				int result = inspectionMapper.addCheckingPlan(spotCheckPlan);
			}
		}
		System.out.println(checkingPlan);
		return null;
	}

	@Override
	public List<SpotCheckPlan> getPlan(String planName) {
		List<SpotCheckPlan>plans = inspectionMapper.getPlan(planName);
		return plans;
	}

	@Override
	public SysUser getUser() {
		SysUser user = SecurityUtils.getLoginUser().getUser();
		return user;
	}

	@Override
	public AjaxResult inspectionRecordUpload(InspectionRecord inspectionRecord) {

		inspectionRecord.setStatus("0");
		inspectionRecord.setMaintenancePersonnel("冯超");
		inspectionRecord.setDeviceName("设备1");

		inspectionMapper.inspectionRecordUpload(inspectionRecord);
		SpotCheckPlan plan = new SpotCheckPlan();
		plan.setInspectionRecordId(inspectionRecord.getInspectionRecordId());
		plan.setId(inspectionRecord.getPlanId());
		inspectionMapper.updatePlan(plan);
		return success();
	}

	@Override
	public AjaxResult faultDeclaration(FaultDeclaration faultDeclaration) {
		SpotCheckPlan spotCheckPlan = new SpotCheckPlan();
		spotCheckPlan.setId(faultDeclaration.getPlanId());
//		SpotCheckPlan spotCheckPlan1 = inspectionMapper.getPlanById(spotCheckPlan);
//		faultDeclaration.setDeviceName(faultDeclaration.getDeviceName());
		faultDeclaration.setStatus("0");
		faultDeclaration.setMaintenancePersonnel("冯超");
        inspectionMapper.faultDeclaration(faultDeclaration);

		SpotCheckPlan plan = new SpotCheckPlan();
		plan.setFaultDeclarationId(String.valueOf(faultDeclaration.getId()));
		plan.setId(faultDeclaration.getPlanId());
		inspectionMapper.updatePlan(plan);
		return success();
	}

	@Override
	public List<FaultDeclaration> getFaultDeclaration() {
		return inspectionMapper.getFaultDeclaration();
	}

	@Override
	public List<InspectionRecord> getInspectionRecord() {
		return inspectionMapper.getInspectionRecord();
	}


	public List<ScheduleShift> convertToScheduleShifts(List<WushuiShift> wushuiShifts) {
		List<ScheduleShift> scheduleShifts = new ArrayList<>();

		// 按照 period 进行分组
		for (WushuiShift shift : wushuiShifts) {
			String period = shift.getPeriod();
			ScheduleShift scheduleShift = findOrCreateScheduleShift(scheduleShifts, period);
			scheduleShift.getShifts().add(shift);
		}

		return scheduleShifts;
	}
	private ScheduleShift findOrCreateScheduleShift(List<ScheduleShift> scheduleShifts, String period) {
		for (ScheduleShift scheduleShift : scheduleShifts) {
			if (scheduleShift.getPeriod().equals(period)) {
				return scheduleShift;
			}
		}
		// 创建新的 ScheduleShift
		ScheduleShift newScheduleShift = new ScheduleShift();
		newScheduleShift.setPeriod(period);
		newScheduleShift.setShifts(new ArrayList<>());
		scheduleShifts.add(newScheduleShift);
		return newScheduleShift;
	}
}
